package com.tongyi.lizhishop.activitys;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.tongyi.lizhishop.constants.Constants;
import com.tongyi.lizhishop.beans.CardListBean;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.MessageApi;
import com.tongyi.lizhishop.beans.CommonResonseBean;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.common.RecycleViewActivity;
import com.tongyi.lizhishop.managers.APIServiceManager;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MyICCardActivity extends RecycleViewActivity implements View.OnClickListener {

    ArrayList dataitem = new ArrayList<CardListBean>();
    private CommonAdapter<CardListBean.ArrayBean> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitleBarView(titlebar, "我的卡包");
        initView();
        initrecycleView();
        refreshLayout.autoRefresh();
    }

    private void initView() {
        getLayoutInflater().inflate(R.layout.activity_my_iccard_bottomview, footer, true);
        findViewById(R.id.btnIcCard).setOnClickListener(this);
    }

    @Override
    public void loadData() {
        APIServiceManager.getInstance().create(MessageApi.class)
                .showMyBank(SPUtils.getInstance().getString("userid"), Constants.perPageSize, currentPageIndex)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CommonObserver<CardListBean>() {
                    @Override
                    public void onSuccess(CardListBean value) {
                        LogUtils.w(value);
                        onActivitySuccess(adapter, dataitem, value.getArray());
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                        showError("请检查网络!");
                    }
                });
    }

    private void initrecycleView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setItemAnimator(new DefaultItemAnimator());
        recyclerview.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter = new CommonAdapter<CardListBean.ArrayBean>(this, R.layout.activity_my_iccard_item, dataitem) {
            @Override
            protected void convert(ViewHolder holder, final CardListBean.ArrayBean o, final int position) {
                String mban_bankname = o.getMban_bankname();
                String mban_username = o.getMban_username();
                String mban_iphone = o.getMban_iphone();
                holder.setText(R.id.bankUserAndIphone, mban_username + " " + mban_iphone);
                holder.setText(R.id.bankName, mban_bankname);
                holder.setText(R.id.time, o.getName());
                holder.getView(R.id.btndelete).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (position == 0 && dataitem.size() == 1) {
                            //用户点击的是最后一个条目
                            multipleStatusView.showEmpty();
                        }
                        dataitem.remove(position);
                        adapter.notifyItemRemoved(position);
                        adapter.notifyItemRangeChanged(position, dataitem.size());
                        notifyWebServer(o);
                    }
                });
            }
        };
        recyclerview.setAdapter(adapter);
    }

    /**
     * 通知服务器删除此银行卡
     */
    private void notifyWebServer(CardListBean.ArrayBean arrayBean) {
        APIServiceManager.getInstance().create(MessageApi.class)
                .deleteMyBank(arrayBean.getMban_id())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CommonObserver<CommonResonseBean>() {
                    @Override
                    public void onSuccess(CommonResonseBean value) {
                        LogUtils.w(value);
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }
                });

    }


    public void onViewClicked() {
        ActivityUtils.startActivity(this, AddIcCardActivity.class);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnIcCard) {
            onViewClicked();
        }
    }

}
